package cn.wolfcode.shop.controller;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;

import cn.wolfcode.shop.service.IProductSkuService;
import cn.wolfcode.shop.service.ISkuPropertyService;
import cn.wolfcode.shop.service.ISkuPropertyValueService;
import cn.wolfcode.shop.vo.GenerateSkuVo;
import cn.wolfcode.shop.vo.JSONResult;
import cn.wolfcode.shop.vo.ProductVo;

@Controller
@RequestMapping("/skus")
public class ProductSkuController {

	@Reference
	private ISkuPropertyService skuPropertyService;

	@Reference
	private ISkuPropertyValueService skuPropertyValueService;

	@Reference
	private IProductSkuService productSkuService;

	/**
	 * 生成出sku数据
	 * /skus/generator
	 * @return
	 */
	@PostMapping("/generator")
	public String generateSku(Model model, @RequestBody GenerateSkuVo vo) {
		//		System.out.println(vo);
		List<Map<String, Object>> skuList = productSkuService.generateSku(vo);
		model.addAttribute("skuList", skuList);
		// 表格的标题是动态的,选择的sku属性中选择那几个
		model.addAttribute("skuPropertyList", vo.getSkuPropertyList());
		return "product_sku/sku_form";
	}

	/**
	 * 保存Sku
	 * @return
	 */
	@PostMapping
	@ResponseBody
	public JSONResult save(ProductVo vo) {
		productSkuService.save(vo);
		return new JSONResult();
	}

}
